package com.lsmw.ucenter.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lsmw.common_service.exceptionhandler.XXException;
import com.lsmw.commonutils.JwtUtils;
import com.lsmw.commonutils.MD5;
import com.lsmw.commonutils.R;
import com.lsmw.ucenter.entity.LsUCenterMember;
import com.lsmw.ucenter.entity.vo.RegisterVo;
import com.lsmw.ucenter.mapper.LsUCenterMemberMapper;
import com.lsmw.ucenter.service.LsUCenterMemberService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author 二号男嘉宾
 * @since 2020-12-24
 */
@Service
public class LsUCenterMemberServiceImpl extends ServiceImpl<LsUCenterMemberMapper, LsUCenterMember> implements LsUCenterMemberService {

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    @Override
    public String login(LsUCenterMember member) {
        if (StringUtils.isBlank(member.getMobile()) || StringUtils.isBlank(member.getPassword())) {
            throw new XXException(403, "登录失败");
        }

        LsUCenterMember uCenterMember = baseMapper.selectOne(new LambdaQueryWrapper<LsUCenterMember>().eq(LsUCenterMember::getMobile, member.getMobile()));
        if (uCenterMember == null) {
            throw new XXException(403, "登录失败");
        }

        if (!MD5.encrypt(member.getPassword()).equals(uCenterMember.getPassword())) {
            throw new XXException(403, "登录失败");
        }

        if (uCenterMember.getIsDisabled() == 1) {
            throw new XXException(403, "登录失败");
        }

        String jwtToken = JwtUtils.getJwtToken(String.valueOf(uCenterMember.getId()), uCenterMember.getNickname(),"7");

        return jwtToken;
    }

    @Override
    public boolean register(RegisterVo registerVo) {
        String code = registerVo.getCode();
        String mobile = registerVo.getMobile();
        String password = registerVo.getPassword();
        String nickname = registerVo.getNickname();

        if (StringUtils.isBlank(mobile) || StringUtils.isBlank(password)
                || StringUtils.isBlank(code) || StringUtils.isBlank(nickname)) {
            throw new XXException(403, "注册失败");
        }

        if (!code.equals(redisTemplate.opsForValue().get(mobile))) {
            throw new XXException(403, "注册失败,验证码错误");
        }

        if (baseMapper.selectCount(new LambdaQueryWrapper<LsUCenterMember>().eq(LsUCenterMember::getMobile, mobile)) > 0) {
            throw new XXException(403, "注册失败,手机号已注册过");
        }

        LsUCenterMember uCenterMember = new LsUCenterMember();
        uCenterMember.setMobile(mobile);
        uCenterMember.setNickname(nickname);
        uCenterMember.setPassword(MD5.encrypt(password));
        uCenterMember.setIsDisabled(0);
        baseMapper.insert(uCenterMember);
        return true;
    }

    @Override
    public LsUCenterMember getMemberInfo(HttpServletRequest request) {
        return baseMapper.selectById(JwtUtils.getUserIdByJwtToken(request));
    }

    /**
     * 普通管理员根据昵称模糊查询(不排除逻辑删除条件)
     * @param nickname
     * @return
     */
    @Override
    public R findMembersByNickname_Admin(String nickname) {
        List<LsUCenterMember> list = this.list(new LambdaQueryWrapper<LsUCenterMember>().like(LsUCenterMember::getNickname, nickname));
        return list.size() != 0
                ?
                R.ok().data("LsUCenterMembers",list)
                :
                R.ok().data("msg","没有找到昵称包含"+nickname+"的用户");
    }

    /**
     * 超级管理员根据昵称模糊查询（排除逻辑删除条件）
     *
     * @param nickname
     * @return
     */
    @Override
    public R findMembersByNickname_superAdmin(String nickname) {
        List<LsUCenterMember> list = baseMapper.findMembersByNicknameAsLikeIsAdmin(new LambdaQueryWrapper<LsUCenterMember>().like(LsUCenterMember::getNickname, nickname));
        return list.size() != 0
                ?
                R.ok().data("LsUCenterMembers",list)
                :
                R.ok().data("msg","没有找到昵称包含"+nickname+"的用户");
    }

    /**
     * 根据会员id删除会员
     *
     * @param memberId
     * @return
     */
    @Override
    public R deleteMemberById(String memberId) {
        LsUCenterMember byId = this.getById(memberId);
        if(byId != null && this.removeById(memberId)){
            return R.ok().data("LsUCenterMember",byId);
        }
        return R.ok().data("msg","请检查id是否正确或联系管理员");
    }

    /**
     * 管理员根据多个会员id删除会员
     *
     * @param memberIds
     * @return
     */
    @Override
    public R deleteMemberByIds(List<String> memberIds) {
        List<LsUCenterMember> lsUCenterMembers = this.listByIds(memberIds);
        if(memberIds.size() == lsUCenterMembers.size() && this.removeByIds(memberIds)){
            return R.ok().data("members",lsUCenterMembers);
        }
        return R.ok().data("msg","请检查id是否正确或联系管理员");
    }
}
